<template>
  <div style="padding: 15px;">
    <div style="margin-bottom: 15px;">
      <Input style="width: 200px;" v-model="loginName" placeholder="请输入管理账号"/>
      <Input style="width: 200px;"  v-model="staffName" placeholder="请输入真实姓名"/>
      <Input style="width: 200px;" v-model="phone" placeholder="请输入联系电话"/>
      <Select style="width:200px" v-model="active" placeholder="请选择状态">
        <Option value="1">正常</Option>
        <Option value="0">禁用</Option>
      </Select>
      <Button @click="searchOrg" type="success">搜索</Button>
      <Button @click="add" type="primary" style="float: right;">添加系统管理员</Button>
    </div>
    <Table border ref="selection" :columns="staffPageTitle" :data="staffPageList"></Table>
    <Page :total="total" show-sizer style="margin-top: 15px;" @on-change="pageChange" @on-page-size-change="pageSizeChange"/>
    <Modal v-model="orgTeacherModal" :title="modalTitle" @on-ok="submit" @on-cancel="cancel">
      <Form :model="orgTeacherForm" :label-width="80">
        <FormItem label="管理账号">
          <Input v-model="orgTeacherForm.loginName" placeholder="请输入管理账号"/>
        </FormItem>
        <FormItem label="真实姓名">
          <Input v-model="orgTeacherForm.staffName" placeholder="请输入真实姓名"/>
        </FormItem>
        <FormItem label="联系电话">
          <Input v-model="orgTeacherForm.mobile" placeholder="请输入联系电话"/>
        </FormItem>
        <FormItem label="管理员">
          <Select v-model="orgTeacherForm.cardno">
            <Option value="1">超级管理员</Option>
            <Option value="0">管理员</Option>
          </Select>
        </FormItem>
        <FormItem label="状态">
          <i-switch size="large" v-model="orgTeacherForm.active" @on-change="statusChange">
            <span slot="open">启用</span>
            <span slot="close">禁用</span>
          </i-switch>
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>

<script>
  import {formatDate} from '_com/date'
  export default {
    name: 'index',
    data () {
      return {
        loginName: '',
        staffName: '',
        phone: '',
        active: '',
        number: 0,
        size: 10,
        total: 0,
        areaModel: '',
        orgTeacherModal: false,
        modalTitle: '',
        orgTeacherForm: {
          loginName: '',
          staffName: '',
          mobile: '',
          cardno: '',
          active: true
        },
        orgAdmin: '',
        staffPageTitle: [
          {
            type: 'selection',
            width: 60,
            align: 'center'
          },
          {
            title: '管理账号',
            key: 'loginName',
            align: 'center'
          },
          {
            title: '真实姓名',
            key: 'staffName',
            align: 'center'
          },
          {
            title: '联系电话',
            key: 'mobile',
            align: 'center'
          },
          {
            title: '创建时间',
            key: 'createTime',
            align: 'center',
            width: 150,
            render: (h, params) => {
              return h('div', formatDate(new Date(params.row.createTime), 'yyyy-MM-dd hh:mm'))
            }
          },
          {
            title: '添加人',
            key: 'createName',
            align: 'center'
          },
          {
            title: '状态',
            key: 'status',
            width: 120,
            align: 'center',
            render: (h, params) => {
              const row = params.row
              const color = row.active === 1 ? 'primary' : row.active === 0 ? 'error' : ''
              const text = row.active === 1 ? '正常' : row.active === 0 ? '禁用' : ''
              return h('Tag', {
                props: {
                  type: 'dot',
                  color: color
                }
              }, text)
            }
          },
          {
            title: '操作',
            key: 'action',
            width: 250,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    type: 'info',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '重置密码',
                        content: '你确定要重置吗？',
                        onOk: () => {
                          this.resetPassword(params.row.id)
                        }
                      })
                    }
                  }
                }, '重置密码'),
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.editor()
                      this.orgTeacherForm.loginName = params.row.loginName
                      this.orgTeacherForm.staffName = params.row.staffName
                      this.orgTeacherForm.mobile = params.row.mobile
                      this.orgTeacherForm.cardno = params.row.cardno
                      this.orgTeacherForm.id = params.row.id
                      if (params.row.active === 1) {
                        this.orgTeacherForm.active = true
                      } else {
                        this.orgTeacherForm.active = false
                      }
                    }
                  }
                }, '编辑'),
                h('Button', {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '删除',
                        content: '你确定要删除吗？',
                        onOk: () => {
                          this.remove(params.row.id)
                        }
                      })
                    }
                  }
                }, '删除')
              ])
            }
          }
        ],
        staffPageList: []
      }
    },
    mounted() {
      this.staffPage()
    },
    methods: {
      staffPage() {
        let url = '/api/sys/staff/page'
        let form = {
          loginName: this.loginName,
          staffName: this.staffName,
          phone: this.phone,
          active: this.active,
          number: this.number,
          size: this.size
        }
        this.$get(url, form).then((res) => {
          if (res.code === 0) {
            this.staffPageList = res.data
          }
        })
      },
      title(title) {
        this.orgTeacherModal = true
        this.modalTitle = title
      },
      add() {
        this.title('添加管理员')
      },
      editor() {
        this.title('编辑管理员')
      },
      submit() {
        if (this.modalTitle === '添加管理员') {
          this.addOrg()
        } else {
          this.theEditor()
        }
      },
      addOrg() {
        let url = '/api/sys/staff/add'
        let form = {
          loginName: this.orgTeacherForm.loginName,
          staffName: this.orgTeacherForm.staffName,
          mobile: this.orgTeacherForm.mobile,
          cardno: this.orgTeacherForm.cardno,
          active: Number(this.orgTeacherForm.active)
        }
        this.$post(url, form).then((res) => {
          if (res.code === 0) {
            this.$Message.success('添加成功')
            this.orgTeacherForm = {}
            this.orgTeacherForm.active = true
            this.staffPage()
          } else {
            this.$Message.error('添加失败')
          }
        })
      },
      resetPassword(id) {
        let url = '/api/sys/staff/resertPassword'
        this.$get(url, {id: id}).then((res) => {
          if (res.code === 0) {
            this.$Message.success('重置成功')
            this.orgTeacherForm = {}
            this.orgTeacherForm.active = true
            this.staffPage()
          } else {
            this.$Message.error('重置失败')
          }
        })
      },
      theEditor() {
        let url = '/api/sys/staff/update'
        let form = {
          loginName: this.orgTeacherForm.loginName,
          staffName: this.orgTeacherForm.staffName,
          mobile: this.orgTeacherForm.mobile,
          cardno: this.orgTeacherForm.cardno,
          active: Number(this.orgTeacherForm.active),
          id: this.orgTeacherForm.id
        }
        this.$put(url, form).then((res) => {
          console.log(res)
          if (res.code === 0) {
            this.$Message.success('修改成功')
            this.orgTeacherForm = {}
            this.orgTeacherForm.active = true
            this.staffPage()
          } else {
            this.$Message.error('修改失败')
          }
        })
      },
      searchOrg() {
        this.staffPage()
      },
      remove(id) {
        let url = '/api/sys/staff/delete'
        this.$deletes(url, {id: id}).then((res) => {
          if (res.code === 0) {
            this.$Message.success('删除成功')
            this.staffPage()
          } else {
            this.$Message.error('删除失败')
          }
        })
      },
      statusChange(status) {
        this.orgTeacherForm.active = status
      },
      cancel() {
        this.orgTeacherForm = {}
        this.orgTeacherForm.active = true
      },
      pageChange(page) {
        this.number = page - 1
        this.staffPage()
      },
      pageSizeChange(page) {
        this.size = page
        this.staffPage()
      }
    }
  }
</script>

<style scoped>

</style>
